from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

# 欢迎抄作业,,,,,

# 初始化 Edge WebDriver
driver = webdriver.Edge(service=Service('D:/作业/大三/下/生产实习/edgedriver_win64/msedgedriver.exe'))

# 打开网页
driver.get('https://blog.csdn.net/rank/list')

# 等待特定元素加载完成
try:
    # 最多等待10秒钟，直到元素出现
    element_present = EC.presence_of_element_located((By.CLASS_NAME, 'hosetitem-title'))
    WebDriverWait(driver, 10).until(element_present)

    # 获取页面源代码并使用BeautifulSoup解析
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')

    # 查找热榜项目
    hot_items = soup.find_all('div', class_='hosetitem-title')

    for item in hot_items:
        title = item.find('a').text.strip()  # 获取标题，strip()去除首尾空白字符
        link = item.find('a')['href']  # 获取链接
        print(f"标题：{title}, 链接：{link}")

finally:
    # 关闭浏览器
    driver.quit()
